# -*- coding:utf-8 -*-
"""
get xiamihot music and artist

<a href="/song/1770154233" title="Give Me Everything">Give Me Everything</a>

<a class="artist" href="/artist/2521" title="">郑融</a>
"""
import urllib
import re

def gethotpage():
    """
    the hot page is http://www.xiami.com/music/hot
    """
    theurl = "http://www.xiami.com/music/hot"
    return urllib.urlopen(theurl).read()

def getallmusic():
    """
    """
    thestr = gethotpage()
    regex = "<a href=\"/song/.*?</a>"
    musics = []
    for musicmatch in re.finditer(regex,thestr):
        tmpdic = {}
        mstr = thestr[musicmatch.start():musicmatch.end()]
        tmpdic['mname'] = mstr[mstr.index(">")+1:mstr.index("<",2)]
        tmpdic['mid'] = mstr[mstr.index("song")+5:mstr.index("\"",10)]
        musics.append(tmpdic)
    return musics

def getallartists():
    thestr = gethotpage()
    regex = "<a class=\"artist.*?</a>"
    artists = []
    for am in re.finditer(regex,thestr):
        astr = thestr[am.start():am.end()]
        tmpdic['aname'] = astr[astr.index(">")+1:astr.index("<",2)]
        tmpdic['aid'] = astr[astr.rindex("artist")+7:astr.index("\"",astr.rindex("ar"))]
        artists.append(tmpdic)
    return artists
